//
//  orderDetailVC.m
//  BeautyPicture
//
//  Created by 沈振邦 on 15/11/25.
//  Copyright © 2015年 szb. All rights reserved.
//

#import "orderDetailVC.h"
#import "orderViewController.h"
#import "UIViewController+MJPopupViewController.h"
#import "BPImageTool.h"
#import "composeVC.h"
#import "AccountTool.h"
#import <ShareSDK/ShareSDK.h>
#import "MBProgressHUD+MJ.h"
#import "productModel.h"
#import "BPProductDetailPage.h"
#import "BPProductModel.h"
#import "AppDelegate.h"
#import "BPProductMakeViewController.h"

#define shareUrl @"http://ttmy.len.so/home/appinvite/workshow/id/"


@interface orderDetailVC ()

@property (nonatomic, copy) NSString *first;
@property (nonatomic,strong)AppDelegate *delegate;


@property (nonatomic, strong) UIView *uiv;
@property (nonatomic, strong) UIVisualEffectView *visualEffect;

@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, copy) NSString *oprice;

@end

@implementation orderDetailVC


- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}

// 隐藏状态栏
- (BOOL)prefersStatusBarHidden {
    
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.array = [NSMutableArray array];
    
    [self setupNav];
    [self createUIView];
    [self createButton];
    
    BPProductDetail *productInfo = self.modelPro;
    
    NSMutableArray *assetArry = [NSMutableArray array];
    
    static dispatch_once_t pred = 0;
    static ALAssetsLibrary *library = nil;
    dispatch_once(&pred,
                  ^{
                      library = [[ALAssetsLibrary alloc] init];
                  });
    
    for (BPProductDetailPage *pages in productInfo.pages) {
        for (BPProductModel *models in pages.moulds) {
            if (models.type == 0) {
                NSURL *assetURl = [NSURL URLWithString:models.alassetStr];
                //重相册库中获取照片
                [library assetForURL:assetURl resultBlock:^(ALAsset *asset) {
                    if (asset == nil) {
                        [assetArry addObject:@"noAsset"];
                    }else{
                        [assetArry addObject:asset];
                    }
                    if (assetArry.count == productInfo.picnum) {
                        _delegate.isEdit = YES;
                        if (assetArry == nil) {
                           
                            [MBProgressHUD showError:@"加载失败"];
                            [self.navigationController popViewControllerAnimated:YES];
                        }else{
                            
                            self.array = assetArry;
                            NSLog(@"从&我的作品&进入%@", self.array);
                        }
                        
                        
                        //BPProductMakeViewController *DIY = [[BPProductMakeViewController alloc] init];
                        //DIY.isFirst = NO;
                       // DIY.productInfo = productInfo;
                        //DIY.alassetArry = assetArry;
                        
                        //[self.navigationController pushViewController:DIY animated:YES];
                    }
                } failureBlock:^(NSError *error) {
                    NSLog(@"dddd");
                }];
            }
        }
    }

    
    
    _popImage = [[BPProductPopUpImage alloc] initWithFrame:CGRectMake(0, 0, BPScreenW, BPScreenH)];
    _popImage.showBigImage.contentMode = UIViewContentModeScaleAspectFit;
    // Do any additional setup after loading the view.
}


// 创建 返回按钮
- (void)createButton{
    
    UIButton *buttonBig = [[UIButton alloc] initWithFrame:CGRectMake(20, 25, 60, 50)];
    buttonBig.backgroundColor = [UIColor clearColor];
    [buttonBig addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonBig];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 23, 23)];
    [button setBackgroundImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    [buttonBig addSubview:button];
    
//    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(20, 25, 23, 23)];
//    [button setBackgroundImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:button];
    
    UIButton *buttonHome = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 40, 25, 23, 23)];
    [buttonHome setBackgroundImage:[UIImage imageNamed:@"homeLeft02"] forState:UIControlStateNormal];
    [buttonHome addTarget:self action:@selector(buttonHome:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonHome];
    
}
// 头部的 两个按钮
- (void)button:(UIButton *)b{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)buttonHome:(UIButton *)home{
    [self.navigationController popToRootViewControllerAnimated:YES];
}



- (void)setupNav{
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithButtonName:@"返回" tatget:self action:@selector(back)];
}


- (void)showBigImage:(NSString *)imageStr
{
    UIImage *image = [UIImage imageWithContentsOfFile:imageStr];
    _popImage.showBigImage.image = image;
    [self szbPopView:_popImage withSize:image.size];
}

- (void)shownetImage:(NSString *)imageStr
{
    BPImageTool *imageTool = [[BPImageTool alloc] init];
    [imageTool loadProcessImage:imageStr withVC:self addPopView:_popImage];
}

- (NSDictionary *)getKong{
    
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"pageStr"] == nil) {
        
        self.kong = [[NSDictionary alloc] init];
        self.kong = @{@"kong":@"666"};
        return self.kong;
    }
    else{
        self.kong = [[NSDictionary alloc] init];
        self.kong = @{@"kong":@"777"};
        return self.kong;
    }
}


- (NSDictionary *)getProductInfo{
    if ([self.productModel.category isEqualToString:@""] && [self.productModel.title isEqualToString:@""] && [self.productModel.finalPrice isEqualToString:@""] && [self.productModel.workID isEqualToString:@""] && [self.productModel.iD isEqualToString:@""] && [self.productModel.oprice isEqualToString:@""] && [self.productModel.xiaoguo isEqualToString:@""] && [self.productModel.price isEqualToString:@""]) {
        
        [MBProgressHUD showError:@"不能继续编辑,数据不完整!"];
        return nil;
        
    }else{
        
        NSDictionary *dic = [NSDictionary dictionary];
        dic = @{@"category" : self.productModel.category,
                @"name" : self.productModel.title,
                @"price" : self.productModel.finalPrice,
                @"workid" : self.productModel.workID,
                @"productid" : self.productModel.iD,
                @"prom":self.productModel.oprice,
                @"xiaoGuo":self.productModel.xiaoguo,
                @"priceS":self.productModel.price,
                };
        self.oprice = self.productModel.oprice;
        
        return dic;
    }
    
}


// 我的作品->继续编辑->保存
- (NSDictionary *)getProductInfoS{
    if ([self.productModel.category isEqualToString:@""] && [self.productModel.title isEqualToString:@""] && [self.productModel.finalPrice isEqualToString:@""] && [self.productModel.workID isEqualToString:@""] && [self.productModel.iD isEqualToString:@""] && [self.productModel.oprice isEqualToString:@""] && [self.productModel.xiaoguo isEqualToString:@""]) {
        [MBProgressHUD showError:@"不能继续编辑,数据不完整!"];
        return nil;
    }else{
        NSDictionary *dic = [NSDictionary dictionary];
        dic = @{@"category" : self.productModel.category,
                @"name" : self.productModel.title,
                @"price" : self.productModel.finalPrice,
                @"workid" : self.productModel.workID,
                @"productid" : self.productModel.iD,
                @"prom":self.productModel.oprice,
                @"xiaoGuo":self.productModel.xiaoguo,
                //@"priceS":self.productModel.price,  // 价格里面 没有 数据
                };
        return dic;
    }
}


/** 继续编辑 */
- (void)normalToDetail{
    NSLog(@"继续编辑");
    
    BPProductDetail *productInfo = self.modelPro;
    NSMutableArray *assetArry = [NSMutableArray array];
    
    static dispatch_once_t pred = 0;
    static ALAssetsLibrary *library = nil;
    dispatch_once(&pred,
                  ^{
                      library = [[ALAssetsLibrary alloc] init];
                  });
    
    for (BPProductDetailPage *pages in productInfo.pages) {
        for (BPProductModel *models in pages.moulds) {
            if (models.type == 0) {
                NSURL *assetURl = [NSURL URLWithString:models.alassetStr];
                //重相册库中获取照片
                [library assetForURL:assetURl resultBlock:^(ALAsset *asset) {
                    if (asset == nil) {
                        [assetArry addObject:@"noAsset"];
                    }else{
                        [assetArry addObject:asset];
                    }
                    if (assetArry.count == productInfo.picnum) {
                        _delegate.isEdit = YES;
                        BPProductMakeViewController *DIY = [[BPProductMakeViewController alloc] init];
                        DIY.isFirst = NO;
                        DIY.productInfo = productInfo;
                        DIY.productInfo.oprice = self.oprice;
                        
                        DIY.alassetArry = assetArry;
                        
                        [self.navigationController pushViewController:DIY animated:YES];
                    }
                } failureBlock:^(NSError *error) {
                    NSLog(@"dddd");
                }];
            }
        }
    }

}


- (NSArray *)getImagePath{
    return _imagePaths;
}


// 作品详情 -> 确认下单按钮
//- (void)pushTonextController
//{
//    orderViewController *rightOrder = [[orderViewController alloc] init];
//    rightOrder.productModel = self.productModel;
//    rightOrder.assetArry = self.assetArry;
//    [self.navigationController pushViewController:rightOrder animated:YES];
//}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}



// 底部 UIView
- (void)createUIView{
    
    CGFloat butX = 80.0f;
    CGFloat butY = self.view.frame.size.height - 40.0f;
    CGFloat butW = self.view.frame.size.width;
    CGFloat butH = 40.0f;
    
    UIView *Aview = [[UIView alloc] initWithFrame:CGRectMake(butX, butY, butW - butX, butH)];
    Aview.backgroundColor = BPGlobaThemeColor;
    [self.view addSubview:Aview];
    
//    UIButton *aView = [[UIButton alloc] initWithFrame:CGRectMake(butX, butY, butW - butX, butH)];
//    aView.backgroundColor = BPGlobaThemeColor;
//    [aView addTarget:self action:@selector(xiaDan:) forControlEvents:UIControlEventTouchDragInside];
//    [aView setTitle:@"马上下单" forState:UIControlStateNormal];
//    [Aview addSubview:aView];
    
    UILabel *aView = [[UILabel alloc] initWithFrame:CGRectMake(Aview.frame.size.width / 2 - 50, 0, 100, butH)];
    aView.text = @"马上下单";
    aView.textAlignment = 1;
    aView.textColor = [UIColor whiteColor];
    [Aview addSubview:aView];
    
    UITapGestureRecognizer *Atap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(xiaDan:)];
    Atap.view.tag = 1;
    [Aview addGestureRecognizer:Atap];
    
    
    
    UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, butY, butX, butH)];
    bView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:bView];
  
    CGFloat LabX = 20.0f;
    CGFloat LabY = 30.0f;
    CGFloat LabW = 40.0f;
    CGFloat LabH = 15.0f;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(LabX, LabY - 2, LabW, LabH)];
    label.text = @"我要分享";
    label.font = [UIFont systemFontOfSize:10];
    label.textColor = [UIColor whiteColor];
    [bView addSubview:label];
    
    UIImageView *ima = [[UIImageView alloc] initWithFrame:CGRectMake(LabX + 5, 0, LabW - 10.0f, LabH + 12.0f)];
    ima.image = [UIImage imageNamed:@"Guixiu"];
    [bView addSubview:ima];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [bView addGestureRecognizer:tap];
    
}

- (void)tap:(UIGestureRecognizer *)taps{
    
//    self.visualEffect = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
//    
//    self.visualEffect.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
//    
//    self.visualEffect.alpha = 0.5;
//    
//    [self.view addSubview:self.visualEffect];
    
    
    self.uiv = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 180, self.view.frame.size.width, 180)];
    self.uiv.alpha = 0.8;
    self.uiv.backgroundColor = [UIColor grayColor];
//    [self.visualEffect addSubview:self.uiv];
    [self.view addSubview:self.uiv];
    
    CGFloat imX = self.uiv.frame.size.width / 4;
    CGFloat imY = 30.0f;
    CGFloat imW = 50;
    CGFloat imH = 50;
    
    UIButton *imageWeiXin = [[UIButton alloc] initWithFrame:CGRectMake(imX, imY, imW, imH)];
    [imageWeiXin setBackgroundImage:[UIImage imageNamed:@"Weixin"] forState:UIControlStateNormal];
    imageWeiXin.backgroundColor = [UIColor whiteColor];
    imageWeiXin.layer.masksToBounds = YES;
    imageWeiXin.layer.cornerRadius = 25;
    [imageWeiXin addTarget:self action:@selector(imageWie:) forControlEvents:UIControlEventTouchUpInside];
    [self.uiv addSubview:imageWeiXin];
    
    UIButton *imagePeng = [[UIButton alloc] initWithFrame:CGRectMake(imX * 2.5, imY, imW, imH)];
    [imagePeng setBackgroundImage:[UIImage imageNamed:@"pengyou"] forState:UIControlStateNormal];
    imagePeng.backgroundColor = [UIColor whiteColor];
    imagePeng.layer.masksToBounds = YES;
    imagePeng.layer.cornerRadius = 25;
    [imagePeng addTarget:self action:@selector(imagePeng:) forControlEvents:UIControlEventTouchUpInside];
    [self.uiv addSubview:imagePeng];
    
    
    
    UILabel *longLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.uiv.frame.size.height - 60, self.uiv.frame.size.width, 1)];
    longLabel.backgroundColor = [UIColor blackColor];
    [self.uiv addSubview:longLabel];
    
    
    UIButton *queDing = [[UIButton alloc] initWithFrame:CGRectMake(0, self.uiv.frame.size.height - 60, self.uiv.frame.size.width, 60)];
    [queDing setTitle:@"取消" forState:UIControlStateNormal];
    [queDing addTarget:self action:@selector(queDing:) forControlEvents:UIControlEventTouchUpInside];
    [queDing setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.uiv addSubview:queDing];
    
    
    
}


- (void)queDing:(UIButton *)queding{
    [self.visualEffect removeFromSuperview];
    [self.uiv removeFromSuperview];
}

// 确认下单 按钮
- (void)xiaDan:(UITapGestureRecognizer *)tap{
//- (void)xiaDan:(id)sender{
    
    // 二次进入 也是会走这个 方法(即 从 "我的作品" 进入)
//    // 要找到从我的作品进入 时候的 assetArr
    if (self.isF == YES) {
        // 我的作品
        orderViewController *rightOrder = [[orderViewController alloc] init];
        rightOrder.productModel = self.productModel;
        rightOrder.productModel.finalPrice = rightOrder.productModel.price;
        rightOrder.assetArry = self.array;
        [self.navigationController pushViewController:rightOrder animated:YES];
    }
    else{
        // 第一次进入
        orderViewController *rightOrder = [[orderViewController alloc] init];
        rightOrder.productModel = self.productModel;
        rightOrder.assetArry = self.assetArry;
        [self.navigationController pushViewController:rightOrder animated:YES];
    }
}



- (void)imageWie:(UIButton *)button{
//    "微信好友"
    [self shareToApp:nil shareType:ShareTypeWeixiSession];
}

- (void)imagePeng:(UIButton *)button{
//    "微信朋友圈分享"
    [self shareToApp:nil shareType:ShareTypeWeixiTimeline];
}


- (void)shareToApp:(id)sender shareType:(ShareType)type
{
    NSLog(@"workid = %@", self.productModel.workID);
    
    NSString *imagep = [[[NSBundle mainBundle] bundlePath]  stringByAppendingPathComponent:@"bpIcon.png"];
    id<ISSContent> publishContent = [ShareSDK content:@"邀请有礼,快领取你的美印币吧"
                                       defaultContent:@""
                                                image:[ShareSDK imageWithPath:imagep]
                                                title:@"天天美印"
                                                  url:[NSString stringWithFormat:@"%@%@",shareUrl,self.productModel.workID]
                                          description:@"邀请有礼"
                                            mediaType:SSPublishContentMediaTypeNews];
    
    
    //创建弹出菜单容器
    id<ISSContainer> container = [ShareSDK container];
    [container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDirectionUp];
    
    id<ISSAuthOptions> authOptions = [ShareSDK authOptionsWithAutoAuth:YES
                                                         allowCallback:YES
                                                         authViewStyle:SSAuthViewStyleFullScreenPopup
                                                          viewDelegate:nil
                                               authManagerViewDelegate:nil];
    
    id<ISSShareOptions> shareOptions = [ShareSDK defaultShareOptionsWithTitle:nil
                                                              oneKeyShareList:[NSArray defaultOneKeyShareList]
                                                               qqButtonHidden:NO
                                                        wxSessionButtonHidden:NO
                                                       wxTimelineButtonHidden:NO
                                                         showKeyboardOnAppear:NO
                                                            shareViewDelegate:nil
                                                          friendsViewDelegate:nil                                                     picViewerViewDelegate:nil];
    
    
    //分享类型
    [ShareSDK showShareViewWithType:type
                          container:container
                            content:publishContent
                      statusBarTips:YES
                        authOptions:authOptions
                       shareOptions:shareOptions
                             result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                 
                                 if (state == SSPublishContentStateSuccess)
                                 {
                                     [MBProgressHUD showSuccess:@"发表成功"];
                                 }
                                 else if (state == SSPublishContentStateFail)
                                 {
                                     [MBProgressHUD showError:@"发表失败"];
                                 }
                             }];
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
